package org.rency.crawler.dal.dao.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.rency.common.utils.enums.YesNoEnum;
import org.rency.crawler.dal.dao.MetaResourceDao;
import org.rency.crawler.dal.entity.MetaResourceDO;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

/**
 * 名称: MetaResourceRepositoryImpl<br>
 * 描述: ${TODO}<br>
 * 类型: JAVA<br>
 * 最近修改时间:2017/8/4 11:10<br>
 *
 * @author CaiyuRen
 * @version [版本号, V1.0]
 * @since 2017/8/4 11:10
 */
@Repository
public class MetaResourceDaoImpl implements MetaResourceDao {

  @Resource
  private MongoTemplate mongoTemplate;

  @Override
  public boolean saveBatch(List<MetaResourceDO> entityList) {
    mongoTemplate.insert(entityList, MetaResourceDO.class);
    return true;
  }

  @Override
  public boolean save(MetaResourceDO entity) {
    try {
      mongoTemplate.save(entity);
      return true;
    } catch (DuplicateKeyException e) {
      return true;
    }
  }

  @Override
  public List<MetaResourceDO> findAll() {
    return mongoTemplate.findAll(MetaResourceDO.class);
  }

  @Override
  public MetaResourceDO load(String id) {
    Criteria criteria = Criteria.where("id").is(id);
    return mongoTemplate.findOne(new Query(criteria),MetaResourceDO.class);
  }

  @Override
  public List<MetaResourceDO> find(String firLevelName) {
    Criteria criteria = Criteria.where("firLevelName").is(firLevelName);
    return mongoTemplate.find(new Query(criteria), MetaResourceDO.class);
  }

  @Override
  public MetaResourceDO find(String firLevelName, String secLevelName, YesNoEnum enable) {
    Criteria criteria = Criteria.where("firLevelName").is(firLevelName);
    if(StringUtils.isNotEmpty(secLevelName)){
      criteria.and("secLevelName").is(secLevelName);
    }
    if(enable != null){
      criteria.and("enable").is(enable.getCode());
    }
    return mongoTemplate.findOne(new Query(criteria), MetaResourceDO.class);
  }

  @Override
  public MetaResourceDO find(String firLevelName, String secLevelName) {
    return this.find(firLevelName,secLevelName,null);
  }

  @Override
  public boolean updateEnable(String id,YesNoEnum newEnable, YesNoEnum oldEnable) {
    Criteria criteria = Criteria.where("id").is(id)
        .and("enable").is(oldEnable.getCode());
    Update update = Update.update("enable", newEnable.getCode());
    UpdateResult result = mongoTemplate.updateFirst(new Query(criteria),update,MetaResourceDO.class);
    return result.getModifiedCount() == 1;
  }

  @Override
  public boolean delete(String id) {
    DeleteResult result = mongoTemplate
        .remove(new Query(Criteria.where("id").is(id)), MetaResourceDO.class);
    return result.getDeletedCount() == 1;
  }
}

